package controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.WebmailModel;

import entities.Webmail;
import entities.Website;

@SuppressWarnings("serial")
public class WebmailDeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		WebmailModel webmailModel = WebmailModel.getInstance();
		Webmail webmail = webmailModel.get(id);
		
		if (webmail != null) {
			webmailModel.delete(webmail);
			
			CookieHelper.setCookie(response, "success", "Delete webmail success: " + id);
		}
		
		response.sendRedirect("/webmail/list");
		return;
	}
}
